﻿#region Header
// ------------------------ Licence / Copyright ------------------------
// 
// ScrumTable for Scrum Meeting Support
// Copyright © HSR - Hochschule für Technik Rapperswil
// All Rights Reserved
// 
// Author:
//  Michael Gfeller Silvan Gehrig Patrick Boos
// 
// ---------------------------------------------------------------------
#endregion

#region Usings

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Windows.Data;
using ScrumTable.Config.ConfigManagement;
using ScrumTable.BL.DM.DataManagement;
using ScrumTable.Config.ElementManagement;
using ScrumTable.Config.UserManagement;
using Microsoft.Practices.Unity;
using ScrumTable.Common;
using ScrumTable.Common.Collections;
using ScrumTable.UI.ViewModel.Data;
using ScrumTable.UI.ViewModel.Extension;

#endregion

namespace ScrumTable.UI.ViewModel.View
{
    /// <summary>
    /// Description of the class
    /// </summary>
    public class DailyScrumIndividualReportViewModel : DynamicUiBase
    {

        #region Declarations
        //--------------------------------------------------------------------
        // Declarations
        //--------------------------------------------------------------------
        private MemberViewModel _currentMember;
        private ObservableCollection<MemberViewModel> _members;
        private ObservableCollection<IScrumItemViewModel<DomainBase>>[] _collections;
        private TargetConfig _config;


        #endregion

        #region Properties
        //--------------------------------------------------------------------
        // Properties
        //--------------------------------------------------------------------  
        /// <summary>
        /// Gets or sets the account manager.
        /// </summary>
        /// <value>The account manager.</value>
        [Dependency]
        public LoginAccountManager AccountManager { get; set; }



        /// <summary>
        /// List of task in the ToDoState
        /// </summary>
        /// <value>List of task in the ToDoState</value>
        public override ObservableCollection<IScrumItemViewModel<DomainBase>>[] Collections
        {
            get
            {
                if (ScrumData.CurrentIteration != null)
                {
                    return _collections;
                }
                return null;
            }
        }

       


        public override TargetConfig Config
        {

            get { return _config; }
            set
            {
                _config = value;
                _collections = new ObservableCollection<IScrumItemViewModel<DomainBase>>[ElementManager.CalcMaxStatesForTarget(value)*2];
                RefreshLists();
            }
        }

        public override IEnumerable<ScrumState> States
        {
            get { return ElementManager.GetStates(Config); }
        }


        /// <summary>
        /// Gets or sets the current member.
        /// </summary>
        /// <value>The current member.</value>
        public MemberViewModel CurrentMember
        {
            get
            {
                if (_currentMember == null && Members.Count > 0)
                {
                    _currentMember = Members.First();
                }
                return _currentMember;
            }
            set
            {
                _currentMember = value;
                SendPropertyChanged("CurrentMember");

                RefreshLists();
            }
        }


        /// <summary>
        /// Gets the members.
        /// </summary>
        /// <value>The members.</value>
        public ObservableCollection<MemberViewModel> Members
        {
            get
            {
                if (ScrumData.CurrentProject != null && _members == null)
                {
                    var random = new Random((int)DateTime.Now.Ticks);
                    var tmpMembers = from x in ScrumData.CurrentMembers orderby random.Next() select x;
                    //tmpMembers.ForEach(x => x.UpdateWorkData());
                    _members = new ObservableCollection<MemberViewModel>(tmpMembers);
                }
                return _members;
            }
        }

        #endregion

        #region Constructors / Destructor
        //--------------------------------------------------------------------
        // Constructors / Destructor
        //--------------------------------------------------------------------


        #endregion

        #region Methods
        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------
    
        /// <summary>
        /// Releases managed (c# or .net) resources.
        /// </summary>
        protected override void ReleaseManagedResources()
        {
           
            base.ReleaseManagedResources();
        }

        protected override void RefreshLists()
        {
            int i = 0;

            foreach (var state in States)
            {
                if (Members.Count > 0)
                {
                    _collections[i] = SetTaskOnList(state, ref _collections[i], ScrumData, x => x.AssignedMember == CurrentMember.Original);
                }
                _collections[i + 1] = SetTaskOnList(state, ref _collections[i + 1], ScrumData, x => x.AssignedMember == null);
                i += 2;
            }
            base.RefreshLists();
        }

        #endregion

        #region Events
        //--------------------------------------------------------------------
        // Events
        //--------------------------------------------------------------------


        protected override void OnContextElementChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e is DomainChangedEventArgs && ((DomainChangedEventArgs)e).ContainsPropertyNames("State", "AssignedMember", "Priority"))
            {
                RefreshLists();
            }
        }

        #endregion
    }
}
